//
//  CLCRL.m
//  LRCAnalyze
//
//  Created by 小草毋正 on 2018/1/5.
//  Copyright © 2018年 zwc. All rights reserved.
//

#import "CLLRC.h"


@implementation CLLRC


- (instancetype)initWithFileUrl:(NSURL *)fileUrl {
    self = [super init];
    if (self) {
        [self analyzeWithFileUrl:fileUrl];
    }
    return self;
}


/** 时间单位为 毫秒  -1表示没有匹配 */
- (NSInteger)indexWithTime:(NSInteger)time {
    NSInteger index = 0;
    for (CLLRCWord *word in self.words) {
        if(index == 0){
            if(word.showTime > time){
                return -1;
            }
        }
        if(word.showTime > time) {
            return index - 1;
        }
        index ++;
    }
    return -1;
}


/** 解析歌词 */
- (BOOL)analyzeWithFileUrl:(NSURL *)fileUrl { 
    
    NSString *wordString = [[NSString alloc] initWithContentsOfURL:fileUrl encoding:NSUTF8StringEncoding error:nil];
    NSArray *words = [wordString componentsSeparatedByString:@"\n"];
    NSMutableArray<CLLRCWord *> *wordsM = [[NSMutableArray alloc] init];
    
    // 先做简单的解析把
    for (NSString *word in words) {
        if([self isMusicInfo:word]){
            [self wordWithMusicInfoContext:word];
        }
        else if([self isMoreTimeWord:word]){
            [wordsM addObjectsFromArray:[self wordsWithMoreTimeContext:word]];
        }
        else if([self isATimeWord:word]){
            [wordsM addObject:[self wordOdSiginTimeContext:word]];
        }
    }
    self.words = [wordsM sortedArrayUsingComparator:^NSComparisonResult(CLLRCWord *word1, CLLRCWord *word2) {
        if(word1.showTime < word2.showTime){
            return NSOrderedAscending;
        }
        else if(word1.showTime == word2.showTime){
            return NSOrderedSame;
        }
        else {
            return NSOrderedDescending;
        }
    }];
    return YES;
}



/** 多个时间 对应的歌词  [00:53.00][01:43.88][02:11.23]虽然无所谓写在脸上*/
- (NSArray<CLLRCWord *> *)wordsWithMoreTimeContext:(NSString *)context {
    context = [context stringByReplacingOccurrencesOfString:@"[" withString:@""];
    NSArray *subWords = [context componentsSeparatedByString:@"]"];
    id objct = nil;
    if(subWords.count >= 2){
        objct = [subWords lastObject];
    }
    NSMutableArray *arrayM = [[NSMutableArray alloc] init];
    for (NSString *subObjcts in subWords) {
        if([subObjcts isEqual:objct] == NO){
            CLLRCWord *word = [[CLLRCWord alloc] init];
            word.showTime   =  [self timeWithContext:subObjcts];
            word.offsetTime = self.offest;
            word.context = objct;
            [arrayM addObject:word];
        }
    }
    return arrayM;
}

/** 只有一个时间的 内容  [02:11.23]虽然无所谓写在脸上*/
- (CLLRCWord *)wordOdSiginTimeContext:(NSString *)context {
    context = [context stringByReplacingOccurrencesOfString:@"[" withString:@""];
    NSArray *subWords = [context componentsSeparatedByString:@"]"]; 
    CLLRCWord *word = [[CLLRCWord alloc] init];
    word.showTime   =  [self timeWithContext:[subWords firstObject]];
    word.offsetTime = self.offest;
    if(subWords.count >= 2){
        word.context = subWords[1];
    }
    return word;
}


/** 根据内容，获取时间 单位时毫秒 mm */
- (NSInteger)timeWithContext:(NSString *)context {
    NSArray *subContexts = [context componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@":."]];
    NSInteger mm = 0;
    for (NSInteger index = 0; index < subContexts.count; index ++) {
        NSString *subString = subContexts[index];
        NSInteger time = [subString integerValue];
        switch (index) {
            case 0: {
                mm += 60 * 1000 * time;
            }
                break;
            case 1: {
                mm += 1000 * time;
            }
                break;
            case 2: {
                mm += 10 * time;
            }
                break;
            default:
                break;
        }
    }
    return mm;
}

//

/** 获取歌曲信息 */
- (void)wordWithMusicInfoContext:(NSString *)context {
    context = [context stringByReplacingOccurrencesOfString:@" " withString:@""]; //去掉空格
    context = [context stringByReplacingOccurrencesOfString:@"[" withString:@""];
    context = [context stringByReplacingOccurrencesOfString:@"]" withString:@""];
    if([context containsString:@"ti:"]){
        context = [context stringByReplacingOccurrencesOfString:@"ti:" withString:@""];
        self.title = context;
    }
    else if ([context containsString:@"ar:"]) {
        context = [context stringByReplacingOccurrencesOfString:@"ar:" withString:@""];
        self.actor = context;
    }
    else if ([context containsString:@"al"]) {
        context = [context stringByReplacingOccurrencesOfString:@"al:" withString:@""];
        self.album = context;
    }
    else if ([context containsString:@"by"]) {
        context = [context stringByReplacingOccurrencesOfString:@"al:" withString:@""];
        self.lcrBy = context;
    }
    else if ([context containsString:@"offset"]) {
        context = [context stringByReplacingOccurrencesOfString:@"offset:" withString:@""];
        self.offest = [context integerValue];
    }
}




/** 是否是一个时间 */
- (BOOL)isATimeWord:(NSString *)word  {
    NSString *regex = @"^\\[\\d{2}:\\d{2}.*\\].*"; // 歌词 一个时间的
    return [self preRegex:regex context:word];
}

/** 是否是多个时间 */
- (BOOL)isMoreTimeWord:(NSString *)word  {
    NSString *regex = @"^\\[\\d{2}:\\d{2}.*\\]\\[\\d{2}:\\d{2}.*\\].*"; // 歌词 多个时间的
    return [self preRegex:regex context:word];
}

/** 判断是否歌曲信息 */
- (BOOL)isMusicInfo:(NSString *)word {
    // 歌曲信息
    NSString *regex = @"^\\[[ti:|ar:|al:|by:|offset:].*\\].*";
    return [self preRegex:regex context:word];
}

/**
 正则表达式
    NSString *regex = @"^\\[[ti:|ar:|al:|by:|offset:].*\\].*";  //歌曲信息
    NSString *regex = @"^\\[\\d{2}:\\d{2}.*\\]\\[\\d{2}:\\d{2}.*\\].*"; // 歌词 多个时间的
    NSString *regex = @"^\\[\\d{2}:\\d{2}.*\\].*"; // 歌词 一个时间的
 */
- (BOOL)preRegex:(NSString *)regex context:(NSString *)context {
    NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [pre evaluateWithObject:context];
}




@end




